/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import WebFeeds 1.0
import com.nokia.meego 1.0
import com.nokia.extras 1.1

SelectionDialog {
    id: serviceSelectionDialog

    ListModel {
        id: serviceListModel
    }
    function updateModel() {
        serviceListModel.clear();
        serviceSelectionDialog.selectedIndex = -1;
        var serviceList = webFeedsApp.services;
        for(var i = 0; i < serviceList.length; ++i) {
            if(!serviceList[i].isAuthenticationDataSet()) {
                serviceListModel.append({"name": serviceList[i].serviceName(), "serviceId": i,
                                                                   "icon": serviceList[i].serviceImageUrl()});
            }
        }
        serviceListModel.append({"name": "Tiny Tiny RSS (coming soon)", "serviceId": -1,
                                                "icon": "qrc:/shared/images/ttrss_account_64x64.png"})
        serviceSelectionDialog.titleText = "Add account";
        serviceSelectionDialog.model = undefined;
        serviceSelectionDialog.model = serviceListModel;
    }

    onStatusChanged: {
        if(serviceSelectionDialog.status == DialogStatus.Opening)
            serviceSelectionDialog.updateModel();
    }

    delegate: Qt.createComponent("SelectionDialogIconDelegate.qml");

    onAccepted: {
        if(selectedIndex >= 0) {
            authenticationSheetLoader.sourceComponent = undefined;
            authenticationSheetLoader.source =
                    webFeedsApp.services[serviceSelectionDialog.model.get(selectedIndex).serviceId].authenticationSheet();
            if(authenticationSheetLoader.status == Loader.Ready) {
                authenticationSheetLoader.item.service = webFeedsApp.services[serviceSelectionDialog.model.get(selectedIndex).serviceId];
                authenticationSheetLoader.item.open();
            }
        }
    }

    Loader {
        id: authenticationSheetLoader
    }
}
